#pragma once
#ifndef QBE_CANVAS_CANVASWIDGET_HPP
#define QBE_CANVAS_CANVASWIDGET_HPP

#include <QGridLayout>
#include <QSize>
#include <QTimer>
#include <QWidget>

#include <spdlog/spdlog.h>
#include <tuple>

#include "../ecs/ecs.hpp"

#include "../adsorb/adsorb.hpp"
#include "../layout/layout.hpp"
#include "../render/render.hpp"
#include "../select/select.hpp"

#include "./RulerWidget.h"

#include "./transferStation.h"
#include "./utils.h"
#include "/ql/common/widgets/progress_bar_display.h"
#include "qbe/src/ui_message/message.h"
#include "qlib/all.h"
#include "vsg/all.h"

class QShortcut;
class QLineEdit;
namespace qbe::canvas {
enum CellState {
  NONE = 0,
  CHANGED = 1,
  DEL = 2,
};

class VsgWindow : public vsgQt::Window {
  // Q_OBJECT
 public:
  VsgWindow(vsg::ref_ptr<vsgQt::Viewer> in_viewer, vsg::ref_ptr<vsg::WindowTraits> in_traits, QWindow* parent);

  vsg::ref_ptr<vsg::Camera> camera;
  qbe::ecs::App* app;
  QWidget* map_widget;

 protected:
  void keyPressEvent(QKeyEvent*) override;
  void keyReleaseEvent(QKeyEvent*) override;
  void mouseMoveEvent(QMouseEvent*) override;
  void mousePressEvent(QMouseEvent*) override;
  void mouseReleaseEvent(QMouseEvent*) override;
  void resizeEvent(QResizeEvent*) override;
  void wheelEvent(QWheelEvent*) override;
  bool isMinimap() const;
};

// class VsgWindow : public vsgQt::Window {
//  public:
//   VsgWindow(){};
//   VsgWindow(vsg::ref_ptr<vsgQt::Viewer> in_viewer, vsg::ref_ptr<vsg::WindowTraits> in_traits, QWindow* parent)
//       : vsgQt::Window(in_viewer, in_traits, parent){};
//   ~VsgWindow(){};

//  protected:
//   void resizeEvent(QResizeEvent*) override {
//     if (!windowAdapter)
//       return;

//     // vsg::clock::time_point event_time = vsg::clock::now();
//     // windowAdapter->bufferedEvents.push_back(vsg::ConfigureWindowEvent::create(windowAdapter, event_time, convert_coord(x()), convert_coord(y()),
//     //                                                                           convert_coord(width()), convert_coord(height())));
//     windowAdapter->resize();
//     if (viewer)
//       viewer->request();
//   };
//   void wheelEvent(QWheelEvent*) override{};
// };

class QLabel;
class CanvasWidget : public QWidget {
  Q_OBJECT

 public:
  CanvasWidget(std::shared_ptr<qlib::Cell> cell, std::shared_ptr<qlib::Library> cell_lib, vsg::ref_ptr<vsgQt::Viewer> viewer,
               vsg::ref_ptr<vsg::WindowTraits> windowTraits, vsg::ref_ptr<vsg::Options> options, QWidget* _map_widget, QWidget* parent = nullptr);
  ~CanvasWidget();

  // void setMinimapWidget(QWidget* widget);
  // void resetMinimapWidget();

  void init_app();
  void test();

  void setEnable(bool e);

  void rulerDraw(const QList<QVector<double>>& lines_x, const QList<QVector<double>>& lines_y, const QVector2D& pos);
  void loadCell(std::string name);

  void wheelEventSlot(QWheelEvent* e);
  //  protected:
  //   void wheelEvent(QWheelEvent*) override;

  void ui_run();

  void arrayOkMessage(qbe::UiMessage::ArrayUi_Ok mes);

  template <typename T>
  void uiSendMessage(const T& t) {
    auto& bus = app.get_bus();
    auto& receive_que = bus.getReceiveQue();

    auto& event = receive_que.emplace_back();
    event = std::make_any<T>(t);
  }

  //设置进度条显示
  void setProgressBarDisplay(bool display);
  //设置值范围
  void setValueRange(int maximum, int minimum);
  //设置当前进度
  void setProgressValue(int value);
  //设置进度条文本
  void setProgressText(QString text);

  bool eventFilter(QObject* obj, QEvent* event) override;

  void setReceiveGlobalKeyEVent(bool receive);

  void setEleInfo(const QString& msg);
  void setOperatorInfo(const QString& msg);

  // 单元状态
  void addCellState(std::shared_ptr<qlib::Cell> cell, CellState state);

  // 重新生成引用渲染数据
  void regenerationReference();

 public:
  QGridLayout* gridLayout;
  RulerWidget* ruler_x_widget;
  RulerWidget* ruler_y_widget;
  qbe::ecs::App app;

  bool recordTime;
  int frame_min_count;
  std::vector<RecordInfoAndFrame> infos_and_frames;
  RecordInfoPtrArray infos;
  qbe::ecs::Time time;
  qbe::ecs::Time totoalTime;
  int index = 0;
  bool realTimeOut = false;
  std::string filePath;

  std::string name;

  qlib::Library::s_ptr library;
  qlib::Cell::s_ptr cell;
  qlib::Cell::s_ptr top_cell;
  vsg::ref_ptr<vsgQt::Viewer> viewer;
  vsg::ref_ptr<vsg::WindowTraits> windowTraits;
  vsg::ref_ptr<vsg::Options> options;
  VsgWindow* window;
  VsgWindow* map_window;
  // vsgQt::Window* window;
  // vsgQt::Window* map_window;
  // MapWindow* map_window;
  QWidget* widget;
  QWidget* map_widget;
  QWidget* t_map_widget;

  QLineEdit* m_ele_info;
  QLineEdit* m_operator_type;

  ProgressBarDisplay* m_progress_bar;

  bool enable{false};
  std::vector<QShortcut*> short_cuts;

  bool receiveGlobalKeyEvent = true;
  unsigned instance_name_max_index = 0;

 signals:
  void sig_run_ui(std::vector<std::any>);
  // void sig_map_window(MapWindow*);

 private:
  QWidget* vsg_widget;
  QVBoxLayout* vsg_layout;
  QTimer* timer_run;
  uint32_t fps = 0;

  std::map<std::shared_ptr<qlib::Cell>, CellState> m_cell_states;  //非当前画板时，记录其他画板修改相关数据引用数据
};

}  // namespace qbe::canvas

#endif
